package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.webUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 14587
 * @date 2020/10/22 9:25
 */
public class BookServlet extends BaseServlet{
    private BookService bookService=new BookServiceImpl();
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数pageNo和pageSize,这里没传入，默认是1
        int pageNo = webUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = webUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用bookService.page(pageNo,pageSize)方法：page对象
        Page<Book> page=bookService.page(pageNo,pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //3.保存page对象到request域中
        req.setAttribute("page",page);
        //4.请求转发到/pages/manager/book_manager.jsp下
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
    /**
     * @author VegetDog
     * @Description 处理添加业务
     * @date 2020-10-22 16:40
     * @param req
 * @param resp
     * @return void
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取请求参数，封装成bean对象
        Book book = webUtils.copyParamToBean(req.getParameterMap(), new Book());
        //2.调用add方法，添加到数据库
        bookService.addBook(book);
        //3.跳转到jsp页面，列表页面，但是还是先访问bookServlet程序
        //这里注意sendRedirect的斜杠是到端口号
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
/**
 * @author VegetDog
 * @Description 处理删除业务
 * @date 2020-10-22 16:40
 * @param req
 * @param resp
 * @return void
 */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //1.获取参数信息
            int id = webUtils.parseInt(req.getParameter("id"), 0);
            //2.调用service.deleteBookById方法
            bookService.deleteBookById(id);
            //3.重定向回显示数据页面，list,获取的工程路径下，没有斜杠
            resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
/**
 * @author VegetDog
 * @Description 处理图书编辑页面的修改操作
 * @date 2020-10-22 16:41
 * @param req
 * @param resp
 * @return void
 */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数并且封装到javabean中
        Book book = webUtils.copyParamToBean(req.getParameterMap(), new Book());
        /*for(Map.Entry<String, String[]> map:req.getParameterMap().entrySet())
        {
            System.out.println(map.getKey()+"="+ Arrays.toString(map.getValue()));
        }*/
        //2.调用bookService.updateBook方法修改图书，
        bookService.updateBook(book);
        //3.回显到list显示页面
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }
/**
 * @author VegetDog
 * @Description 用于页面的信息获取
 * @date 2020-10-22 16:41
 * @param req
 * @param resp
 * @return void
 */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.通过bookService获取图书信息
        List<Book> books = bookService.queryBooks();
        //2.写入request域中
        req.setAttribute("books",books);
        //3.转到book/manager/book_manager.jsp中
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
/**
 * @author VegetDog
 * @Description 用于编辑页面的修改操作
 * @date 2020-10-22 16:43
 * @param req
 * @param resp
 * @return void
 */
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数信息
        int id = webUtils.parseInt(req.getParameter("id"),0);
        //2.调用book.queryBookById方法查询图书
        Book book = bookService.queryBookById(id);
        //3.存在request域中
        req.setAttribute("book",book);
        //4.跳转到jsp页面显示
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }
}
